package com.example.helloworld;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class Looper extends Activity {

	private EditText editText01, editText02;
	
	MainHandler mainHandler;
	ProcessThread processThread;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_looper);
		
		mainHandler =	new MainHandler();
		processThread =	new ProcessThread();
		
		editText01 =	(EditText)findViewById(R.id.editText01);
		editText02 =	(EditText)findViewById(R.id.editText02);
		
		Button button =	(Button)findViewById(R.id.process);
		button.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				String string =	editText01.getText().toString();
				Message message =	Message.obtain();
				message.obj =		string;
				
				processThread.handler.sendMessage(message);
			}
		});
		processThread.start();
	}

	class ProcessThread extends Thread {
		ProcessHandler handler;
		
		public ProcessThread() {
			handler =	new ProcessHandler();
		}
		
		public void run() {
			android.os.Looper.prepare();
			android.os.Looper.loop();
		}
	}
	
	class ProcessHandler extends Handler {
		@Override
		public void handleMessage(Message msg) {
			Message message =	Message.obtain();
			message.obj =		msg.obj + " ProcessHandler";
			
			mainHandler.sendMessage(message);
		}
	}
	
	class MainHandler extends Handler {
		@Override
		public void handleMessage(Message msg) {
			String string =	(String)msg.obj;
			editText02.setText(string);
		}
	}
}
